package com.FH.login;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by administor on 2017/1/3.
 */
public class ExtInterfaceCall {
	//外部接口调用post方法
	public static String postHttp(String url,String datajson) throws IOException {
	    String result = "";
		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "application/json");
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("POST");

		OutputStream outputStream = conn.getOutputStream();
		outputStream.write(datajson.getBytes());
		outputStream.flush();
		BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));
		String output;
        while ((output = responseBuffer.readLine()) != null) {
            System.out.println(output);
            result += output;
        }

		conn.disconnect();
		return result;
	}
	//调用get接口
	public static String getHttp(String url) throws IOException{

		String result = "";
		HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
		conn.setRequestProperty("Accept", "application/json");
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
//		int code = conn.getResponseCode();
        InputStream inStream =  conn.getInputStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

        String inputLine;

        while ((inputLine = in.readLine()) != null){
        	System.out.println(in.readLine());
        	result += inputLine;
        }

        in.close();

        return result;
	}


}
